class Solution
{
public:
    int triangleNumber(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        int ans = 0;
        for (int max = n - 1; max > 1; max--)
        {
            int left = 0;
            int right = max - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > nums[max])
                {
                    ans += (right - left);
                    right--;
                }
                else
                {
                    left++;
                }
            }
        }
        return ans;
    }
};